/**
 * Copyright (C), 2015-2019, XXX有限公司
 * FileName: AlipayRequest
 * Author:   zhouheng
 * Date:     2019/5/15 23:12
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.star.pay.service.core.processor.channel.alipay;

import com.star.pay.service.core.config.PaymentConfig;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.SortedMap; /**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author zhouheng
 * @create 2019/5/15
 * @since 1.0.0
 */
public class AlipayRequest {

    /**
     * 生成要请求给支付宝的参数数组
     * @param sParaTemp 请求前的参数数组
     * @return 要请求的参数数组
     */
    private static Map<String, Object> buildRequestPara(Map<String, Object> sParaTemp, PaymentConfig aliConfig) {
        //除去数组中的空值和签名参数
        Map<String, Object> sPara = AlipayCore.paraFilter(sParaTemp);
        //生成签名结果
        String mysign = buildRequestMysign(sPara, aliConfig);

        //签名结果与签名方式加入请求提交参数组中
        sPara.put("sign", mysign);
        sPara.put("sign_type", aliConfig.getSign_type());

        return sPara;
    }

    /**
     * @description: 生成签名结果
     * @date: 2019/5/15 23:35
     * @author:zhouheng
     */
    private static String buildRequestMysign(Map<String, Object> sPara, PaymentConfig aliConfig) {
        String preStr = AlipayCore.createLinkString(sPara);
        String mysign = "";
        if(aliConfig.getSign_type().equals("MD5") ) {
            mysign = MD5.sign(preStr, aliConfig.getPrivate_key(), aliConfig.getInput_charset());
        }
        return mysign;
    }


    /**
     * 生成要请求给支付宝的参数数组
     * @param sParaTemp 请求前的参数数组
     * @return 要请求的参数数组
     */
    public static Map<String,Object> buildRequestParam(SortedMap<String, Object> sParaTemp, PaymentConfig paymentConfig) {
        //待请求参数数组
        Map<String, Object> sPara = buildRequestPara(sParaTemp, paymentConfig);

        return sPara;
    }

    /**
     * 建立请求，以表单HTML形式构造（默认）
     * @param sParaTemp 请求参数数组
     * @param strMethod 提交方式。两个值可选：post、get
     * @param strButtonName 确认按钮显示文字
     * @return 提交表单HTML文本
     */
    public static String buildRequest(SortedMap<String, Object> sParaTemp, String strMethod, String strButtonName, PaymentConfig aliConfig) {
        //待请求参数数组
        Map<String, Object> sPara = buildRequestPara(sParaTemp, aliConfig);
        List<String> keys = new ArrayList<String>(sPara.keySet());
        StringBuffer sbHtml = new StringBuffer();
        sbHtml.append("<meta http-equiv='content-type' content='text/html; charset=utf-8'>");
        sbHtml.append("<form id=\"alipaysubmit\" name=\"alipaysubmit\" accept-charset=\"utf-8\" action=\"" + aliConfig.getPay_gateway_new()
                + "_input_charset=" + aliConfig.getInput_charset() + "\" method=\"" + strMethod
                + "\">");

        for (int i = 0; i < keys.size(); i++) {
            String name = (String) keys.get(i);
            String value = (String) sPara.get(name);

            sbHtml.append("<input type=\"hidden\" name=\"" + name + "\" value=\"" + value + "\"/>");
        }

        //submit按钮控件请不要含有name属性
        sbHtml.append("<input type=\"submit\" value=\"" + strButtonName + "\" style=\"display:none;\"></form>");
        sbHtml.append("<script>document.forms['alipaysubmit'].submit();</script>");

        return sbHtml.toString();
    }
}